﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using AMS.UI.Controls;
using AMS.ComponentModel;

namespace AMSTrademarkClient
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            InitFrmMainModel();
            tsProgress.Visible = false;
            tsMain.Tabs.Clear();
            LoadWorkForms();
        }

        void InitFrmMainModel()
        {
            FrmMainModel.Instance.MainForm = this;
            FrmMainModel.Instance.MenuBar = menuStrip1;
            FrmMainModel.Instance.ProgressBar = tsProgress;
            FrmMainModel.Instance.StatusBar = statusStrip1;
            ////FrmMainModel.Instance.(navPanel);
            ////FrmMainModel.Instance.Init_SetProgressBar(tsProgress);
        }

        void LoadWorkForms()
        {
            //ShowForm(new FrmMyTasks());
            //ShowForm(new Cases.FrmCaseRegisterChannel());
            //ShowForm(new Cases.FrmEditRegisterCase());
        }

        void ShowForm(Form frm)
        {
            TabControlPanel panel = new TabControlPanel();
            TabItem item = new TabItem(this.components);
            item.Text = frm.Text;
            item.Icon = frm.Icon;
            item.AttachedControl = panel;
            item.Tag = frm.Tag;
            panel.TabItem = item;
            panel.Dock = DockStyle.Fill;
            ////tabMain.Tabs.Add(item);
            ////tabMain.Controls.Add(panel);
            ShowForm(frm, panel);
        }

        void ShowForm(Form frm, TabControlPanel panel)
        {
            frm.MdiParent = this;
            frm.Parent = panel;
            panel.Tag = frm.Tag;
            frm.FormBorderStyle = FormBorderStyle.None;
            frm.Dock = DockStyle.Fill;
            frm.AutoScroll = true;
            frm.Show();
        }

        private void tabMain_TabItemClose(object sender, TabStripActionEventArgs e)
        {
            bool canClose = true;
            ////if (tabMain.SelectedPanel != null)
            ////{
            ////    Control panel = tabMain.SelectedPanel;
            ////    if (panel.Controls.Count > 0)
            ////    {
            ////        Form frm = panel.Controls[0] as Form;
            ////        if (frm != null)
            ////        {
            ////            IWorkControl ctrl = frm as IWorkControl;
            ////            if (ctrl != null)
            ////            {
            ////                canClose = ctrl.ConfirmClose();
            ////            }

            ////            if (canClose)
            ////            {
            ////                tabMain.Controls.Remove(panel);
            ////                frm.Close();
            ////            }
            ////        }
            ////        else
            ////        {
            ////            tabMain.Controls.Remove(panel);
            ////        }
            ////    }
            ////    else
            ////    {
            ////        tabMain.Controls.Remove(panel);
            ////    }
            ////}

            e.Cancel = !canClose;
        }

        private void lkHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }

        private void btnMenuItem_Click(object sender, EventArgs e)
        {
            string tag;
            if (sender is Control)
            {
                tag = (sender as Control).Tag as string;
            }
            else if (sender is BaseItem)
            {
                tag = (sender as BaseItem).Tag as string;
            }
            else
            {
                throw new NotImplementedException("Not impleted for type: " + sender.GetType().ToString());
            }

            Cursor = Cursors.WaitCursor;
            try
            {
                ShowForm(tag);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        private void ShowForm(string tag)
        {
            Type formType = ObjectUtility.GetType(tag);
            FrmMainModel.Instance.CheckAndShowForm(formType, tag);
        }
    }
}
